home *** CD-ROM | disk | FTP | other *** search
- /*------------------------------------------------------------------------------
- File: CScripter.cpp
-
- Contains: The CScripter class implementation.
-
- Written by: Sue Dumont, Andrey Dolgachev
-
- Copyright: © 1995 by Apple Computer, Inc., all rights reserved.
- ------------------------------------------------------------------------------*/
-
- // -- Compiler/Preprocessor Notification --
-
- #ifndef _COMPILERDEFS_
- #include "CompDefs.h"
- #endif
-
- // -- OpenDoc Utilities --
-
- #ifndef _EXCEPT_
- #include <Except.h>
- #endif
-
- // -- CScripter Includes --
-
- #ifndef _CSCRIPTER_
- #include "CScripter.h"
- #endif
-
- #ifndef SOM_SampleCode_DataTransferExt_xh
- #include <DataTransferExt.xh>
- #endif
-
- // -- OpenDoc Utilities --
-
- #ifndef _BARRAY_
- #include <BArray.h>
- #endif
-
- #ifndef _ODMEMORY_
- #include <ODMemory.h>
- #endif
-
- #ifndef _ODUTILS_
- #include <ODUtils.h>
- #endif
-
- #ifndef _ODDESUTL_
- #include <ODDesUtl.h>
- #endif
-
- #ifndef _ISOSTR_
- #include <ISOStr.h>
- #endif
-
- #ifndef _TEMPOBJ_
- #include <TempObj.h>
- #endif
-
-
- // -- Mac Includes --
-
- #ifndef __APPLESCRIPT__
- #include <AppleScript.h>
- #endif
-
- #ifndef __COMPONENTS__
- #include <Components.h>
- #endif
-
- #ifndef __OSA__
- #include <OSA.h>
- #endif
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- #pragma segment CScripter
-
- //==============================================================================
- // CScripter
- //==============================================================================
-
- //------------------------------------------------------------------------------
- // Method: CScripter
- //------------------------------------------------------------------------------
- CScripter::CScripter()
- {
- fComponent = kODNULL;
- fSource = kODNULL;
- fScriptID = kOSANullScript;
- fResultValue = kOSANullScript;
- fScriptDirty = kODFalse;
- }
-
- //------------------------------------------------------------------------------
- // Method: ~CScripter
- //------------------------------------------------------------------------------
- CScripter::~CScripter()
- {
- // Dispose of the source data, script data, and result value.
- if ( fSource )
- AEDisposeDesc(fSource);
- if ( fComponent )
- {
- OSADispose((ComponentInstance)fComponent, fScriptID);
- OSADispose((ComponentInstance)fComponent, fResultValue);
-
- // Terminate the connection to the scripting component.
- CloseComponent((ComponentInstance)fComponent);
- }
- }
-
- //------------------------------------------------------------------------------
- // Method: OpenScriptingComponent
- //
- // Description: Open a generic scripting component.
- //------------------------------------------------------------------------------
- ODScriptingConnection CScripter::OpenScriptingComponent()
- {
- TRY
- // We need to open only one connection, so first check to make sure
- // we haven't already done so.
- if ( fComponent == kODNULL )
- fComponent = (ODScriptingConnection) OpenDefaultComponent(kOSAComponentType,
- kOSAGenericScriptingComponentSubtype);
- CATCH_ALL
- RERAISE;
- ENDTRY
-
- return fComponent;
- }
-
- //------------------------------------------------------------------------------
- // Method: ReleaseScriptData
- //------------------------------------------------------------------------------
- void CScripter::ReleaseScriptData()
- {
- if ( fScriptID != kOSANullScript )
- {
- OSADispose((ComponentInstance) fComponent, fScriptID);
- fScriptID = kOSANullScript;
- }
-
- fScriptDirty = kODTrue;
- }
-
- //------------------------------------------------------------------------------
- // Method: GetScriptSourceDesc
- //
- // Description: Return an OD descriptor with a reference to the current script source.
- //------------------------------------------------------------------------------
- ODDesc* CScripter::GetScriptSourceDesc()
- {
- ODDesc* result = kODNULL;
-
- ODVolatile(result);
-
- TRY
- result = new ODDesc;
- AEDescToODDesc(fSource, result);
- CATCH_ALL
- // Dispose of the descriptor record and set the pointer to null.
- ODDeleteObject(result);
- RERAISE;
- ENDTRY
-
- return result;
- }
-
- //------------------------------------------------------------------------------
- // Method: SetScriptSourceDesc
- //
- // Description: Set the current script source with the given descriptor.
- //------------------------------------------------------------------------------
- void CScripter::SetScriptSourceDesc( ODDesc* source )
- {
- TRY
- if ( fSource == kODNULL )
- fSource = new AEDesc; // Initialize fSource.
- else
- // Deallocate fSource and set it to a null descriptor record.
- AEDisposeDesc(fSource);
-
- ODDescToAEDesc(source, fSource);
-
- // Set the dirty flag to update the script object.
- fScriptDirty = kODTrue;
- CATCH_ALL
- RERAISE;
- ENDTRY
- }
-
- //------------------------------------------------------------------------------
- // Method: GetScriptSourceBA
- //
- // Description: Attempts to return data in the type requested. Styled text
- // and plain text are supported.
- //------------------------------------------------------------------------------
- ODBoolean CScripter::GetScriptSourceBA( ODPlatformType wantType, ODByteArray* data )
- {
- DescType descType;
- ODSLong dataSize, styleSize;
- ODPtr dataPtr;
- ODBoolean result = kODFalse;
-
- TRY
- descType = fSource->descriptorType;
-
- if ( descType == typeStyledText || descType == typeAEText )
- {
- AERecord record;
-
- TempAEDesc textDesc = new AEDesc;
- TempAEDesc styleDesc = new AEDesc;
-
- // Coerce to record and extract text and style fields:
- if ( !AECoerceDesc(fSource, typeAERecord, &record) )
- {
- AEGetKeyDesc(&record, keyAEStyles, typeScrapStyles, styleDesc);
- AEGetKeyDesc(&record, keyAEText, typeChar, textDesc);
- }
-
- AEDisposeDesc(&record);
-
- if ( wantType == 'stxt' )
- {
- styleSize = GetHandleSize(styleDesc->dataHandle);
- dataSize = GetHandleSize(textDesc->dataHandle) + styleSize;
-
- // Copy the data into the ODByteArray
- data->_buffer = (octet*) ODNewPtrClear(dataSize);
-
- dataPtr = ODLockHandle(styleDesc->dataHandle);
- ODBlockMove(dataPtr, data->_buffer, styleSize);
- ODUnlockHandle(styleDesc->dataHandle);
- dataPtr = ODLockHandle(textDesc->dataHandle);
- ODBlockMove(dataPtr, data->_buffer+styleSize, dataSize - styleSize);
- ODUnlockHandle(textDesc->dataHandle);
-
- data->_length = dataSize;
- data->_maximum = dataSize;
-
- result = kODTrue;
- }
- else // return plain text
- {
- dataSize = GetHandleSize(textDesc->dataHandle);
-
- // Copy the data into the ODByteArray
- data->_buffer = (octet*) ODNewPtrClear(dataSize);
- dataPtr = ODLockHandle(fSource->dataHandle);
- ODBlockMove(dataPtr, data->_buffer, dataSize);
- ODUnlockHandle(fSource->dataHandle);
-
- data->_length = dataSize;
- data->_maximum = dataSize;
-
- result = kODTrue;
- }
- }
- CATCH_ALL
- // Clean up allocated memory.
- if ( data )
- DisposeByteArray(data);
- result = kODFalse;
- RERAISE;
- ENDTRY
-
- return result;
- }
-
- //------------------------------------------------------------------------------
- // Method: SetScriptSourceBA
- //
- // Description: Set the current script source from the specified ODByteArray.
- // The ODByteArray contains the data, data type and size..
- //------------------------------------------------------------------------------
- void CScripter::SetScriptSourceBA( ODPlatformType type, ODByteArray* source)
- {
- ODHandle data;
-
- TRY
- data = ODNewHandle(source->_length);
- ODPtr dataPtr = ODLockHandle(data);
- ODBlockMove(source->_buffer, dataPtr, source->_length);
- ODUnlockHandle(data);
-
- if ( fSource == kODNULL )
- fSource = new AEDesc; // Initialize fSource.
- else
- // Deallocate fSource and set it to a null descriptor record.
- AEDisposeDesc(fSource);
-
- // Set fSource to the current script data.
- fSource->descriptorType = type;
- fSource->dataHandle = data;
-
- // Set dirty flag to update the script object.
- fScriptDirty = kODTrue;
- CATCH_ALL
- RERAISE;
- ENDTRY
- }
-
- //------------------------------------------------------------------------------
- // Method: SetOSAScriptID
- //------------------------------------------------------------------------------
- void CScripter::SetOSAScriptID( ODOSAID scriptID )
- {
- // First clean up, if necessary.
- if ( fScriptID != kOSANullScript )
- OSADispose((ComponentInstance)fComponent, fScriptID);
-
- fScriptID = scriptID;
- fScriptDirty = kODFalse;
- }
-
- //------------------------------------------------------------------------------
- // Method: DoCompile
- //
- // Description: Compile the script using the current script source
- // and current script object id.
- //------------------------------------------------------------------------------
- void CScripter::DoCompile()
- {
- THROW_IF_ERROR( OSACompile((ComponentInstance)fComponent, fSource,
- kOSAModeCompileIntoContext, &fScriptID) );
- fScriptDirty = kODFalse;
- }
-
- //------------------------------------------------------------------------------
- // Method: DoDecompile
- //
- // Description: Decompile the script data into source data.
- //------------------------------------------------------------------------------
- void CScripter::DoDecompile()
- {
- THROW_IF_ERROR( OSAGetSource( (ComponentInstance)fComponent, fScriptID,
- typeStyledText, fSource) );
- }
-
- //------------------------------------------------------------------------------
- // Method: DoRun
- //------------------------------------------------------------------------------
- void CScripter::DoRun()
- {
- TRY
- // if the script object is not current, compile it.
- if ( fScriptDirty )
- this->DoCompile();
-
- // If compilation was successful, then execute the script
- THROW_IF_ERROR( OSAExecute((ComponentInstance)fComponent,
- fScriptID,
- kOSANullScript,
- kOSAModeNull,
- &fResultValue) );
- CATCH_ALL
- RERAISE;
- ENDTRY
- }
-
- //------------------------------------------------------------------------------
- // Method: GetResultAsODDesc
- //------------------------------------------------------------------------------
- void CScripter::GetResultAsODDesc( ODDescType type, ODDesc* result )
- {
- TRY
- TempAEDesc newDesc = new AEDesc;
-
- // Convert the script value to text.
- OSADisplay( (ComponentInstance) fComponent, fResultValue,
- type, kOSAModeNull, newDesc);
- AEDescToODDesc(newDesc, result);
- CATCH_ALL
- RERAISE;
- ENDTRY
- }
-
- //------------------------------------------------------------------------------
- // Method: GetResultAsText
- //------------------------------------------------------------------------------
- ODISOStr CScripter::GetResultAsText( Environment* ev )
- {
- ODByteArray data;
- ODDesc* rDesc = kODNULL;
- TempODISOStr result = kODNULL;
-
- ODVolatile(rDesc);
- ODVolatile(result);
-
- TRY
- if ( fResultValue != kOSANullScript )
- {
- rDesc = new ODDesc;
- this->GetResultAsODDesc(typeChar, rDesc );
-
- if ( rDesc->GetDescType(ev) != typeNull )
- {
- data = rDesc->GetRawData(ev);
- result = (ODISOStr) ODNewPtr(data._length + 1);
- ODISOStrNCopy(result, (ODISOStr)data._buffer, data._length);
- ((char*)result)[data._length] = '\0';
-
- ODDeleteObject(rDesc);
- }
- }
- CATCH_ALL
- ODDeleteObject(rDesc);
- RERAISE;
- ENDTRY
-
- return result.DontDelete();
- }
-
- //------------------------------------------------------------------------------
- // Method: GetError
- //------------------------------------------------------------------------------
- void CScripter::GetError( ODDescType kind, ODDescType type, ODDesc* result )
- {
- TRY
- TempAEDesc newDesc = new AEDesc;
-
- OSAScriptError((ComponentInstance)fComponent, kind, type, newDesc);
- AEDescToODDesc(newDesc, result);
- CATCH_ALL
- RERAISE;
- ENDTRY
- }
-
- //------------------------------------------------------------------------------
- // Method: GetErrorMessage
- //------------------------------------------------------------------------------
- ODISOStr CScripter::GetErrorMessage( Environment* ev )
- {
- ODByteArray data;
- ODDesc* rDesc = kODNULL;
- TempODISOStr result = kODNULL;
-
- ODVolatile(rDesc);
- ODVolatile(result);
-
- TRY
- rDesc = new ODDesc;
- this->GetError(kOSAErrorMessage, typeChar, rDesc);
-
- data = rDesc->GetRawData(ev);
- result = (ODISOStr) ODNewPtr(data._length + 1);
- ODISOStrNCopy(result, (ODISOStr)data._buffer, data._length);
- ((char*)result)[data._length] = '\0';
- CATCH_ALL
- if ( result )
- {
- ODDisposePtr(result);
- result = kODNULL;
- }
- RERAISE;
- ENDTRY
-
- ODDeleteObject(rDesc);
-
- return result.DontDelete();
- }
-
- //------------------------------------------------------------------------------
- // Method: GetErrorAppName
- //------------------------------------------------------------------------------
- ODISOStr CScripter::GetErrorAppName( Environment* ev )
- {
- ODByteArray data;
- ODDesc* rDesc = kODNULL;
- TempODISOStr result = kODNULL;
-
- ODVolatile(rDesc);
- ODVolatile(result);
-
- TRY
- rDesc = new ODDesc;
- this->GetError(kOSAErrorApp, typeChar, rDesc);
-
- data = rDesc->GetRawData(ev);
- result = (ODISOStr) ODNewPtr(data._length + 1);
- ODISOStrNCopy(result, (ODISOStr)data._buffer, data._length);
- ((char*)result)[data._length] = '\0';
- CATCH_ALL
- if ( result )
- {
- ODDisposePtr(result);
- result = kODNULL;
- }
- RERAISE;
- ENDTRY
-
- ODDeleteObject(rDesc);
-
- return result.DontDelete();
- }
-